﻿using Microsoft.AspNetCore.Mvc;
using MyDay13.IService;
using MyDay13.Models;
using System.Linq;
using System.Data;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;

namespace MyDay13.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        IUserService user;
        Microsoft.AspNetCore.Hosting.IWebHostEnvironment _host;
       public UserController(IUserService userService, Microsoft.AspNetCore.Hosting.IWebHostEnvironment host)
        {
            user = userService;
            _host = host;
        }
        [HttpPost]
        [Route("UserAdd")]
        public IActionResult UserAdd(D13User d13User)
        {
            return Ok(user.UserAdd(d13User));
        }
        [HttpGet]
        [Route("UserDel")]
        public IActionResult UserDel(string ids)
        {
            return Ok(user.UserDel(ids));
        }
        [HttpPost]
        [Route("UserUpt")]
        public IActionResult UserUpt(D13User d13User)
        {
            return Ok(user.UserUpt(d13User));
        }
        [HttpGet]
        [Route("UserShow")]
        [Authorize]
        public IActionResult UserShow(int pageindex,int pagesize,string name="",string acc="")
        {
            var list = user.UserShow();
            var count = list.Count();
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(x => x.UserName.Contains(name)).ToList();
            }
            if (!string.IsNullOrEmpty(acc))
            {
                list = list.Where(x => x.Account.Contains(acc)).ToList();
            }
            var slist= list.OrderBy(x => x.UId).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();
            return Ok(new {list=slist,total=count });
        }
       /* [HttpPost]
        [Route("UpData")]
        public  async Task <IActionResult> UpData()
        {
            var files = Request.Form.Files;
            var filenameList = await new Common.MyFileUploadHelper(_host).UploadAsync(files, "employee");
            return Ok(filenameList);
        }*/
    }
}
